<template>
    <div class="boxs">
        <el-card>
            <!-- <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-form-item label="appName">
                    <el-select v-model="queryParams.package_name" @change="handleQuery">
                        <el-option v-for="item in pkList" :key="item.package_name" :label="item.shortName"
                            :value="item.package_name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="status">
                    <el-select v-model="queryParams.status" @change="handleQuery">
                        <el-option v-for="item in stateList" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="update_time">
                    <el-date-picker v-model="value1" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
                        :start-placeholder="$t('table.start')" :end-placeholder="$t('table.end')" @change="timeChange">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="small"
                        @click="handleQuery">{{ $t('table.search') }}</el-button>
                    <el-button icon="el-icon-refresh" size="small"
                        @click="resetQuery">{{ $t('table.reset') }}</el-button>
                </el-form-item>
            </el-form> -->
            <!-- <div class="flex_between">
                <div></div>
                <el-button type="primary" size="small" @click="goForm">Add+</el-button>
            </div> -->

            <el-table border :data="moreArr" style="margin-top: 15px" v-loading="loading">
                <el-table-column label="index" align="center" width="60" type="index" />
                <el-table-column label="appName" align="center" width="140" prop="app_name">
                </el-table-column>
                <el-table-column label="version" width="90" align="center" prop="version" />

                <el-table-column label="type" width="90" align="center" prop="type" />
                <el-table-column label="icon" align="center" prop="icon" width="150">
                    <template slot-scope="scope">
                        <img :src="scope.row.icon" alt="Image"
                            style="max-width: 100px; max-height: 100px; cursor: pointer;"
                            @click="openDialog(scope.row.icon)" />
                    </template>
                </el-table-column>

                <el-table-column label="shortDescription" align="center" prop="shortDescription">
                    <template slot-scope="scope">
                        <div style="cursor: pointer; color: blue; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" @click="copyEmail1(scope.row.shortDescription)">
                            {{ scope.row.shortDescription }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="longDescription" align="center" prop="longDescription">
                    <template slot-scope="scope">
                        <div style="cursor: pointer; color: blue; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" @click="copyEmail1(scope.row.longDescription)">
                            {{ scope.row.longDescription }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="v-updateTime" width="160" align="center" prop="version_update_date">
                </el-table-column>
                <el-table-column label="create_date" align="center" width="160" prop="create_date" />
                <el-table-column label="update_date" align="center" width="160" prop="update_date" />
                <!-- <el-table-column :label="$t('feedback.operate')" align="center" width="250">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">open</el-button>
                        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">edit</el-button>
                        <el-button size="mini" @click="handleDelete(scope.$index, scope.row)">delete</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.size"
                @pagination="getMore" />
        </el-card>

        <el-dialog :title="$t('feedback.operate')" :visible.sync="dialogVisible" width="800px">
            <el-form class="demo-form-inline" label-width="140px" :model="form" :rules="rules" ref="ruleForm">
                <el-row>
                    <el-col :span="12">
                        <!-- type 字段 -->
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="form.type" placeholder="请选择类型">
                                <el-option label="android" value="android"></el-option>
                                <el-option label="ios" value="ios"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <!-- chat_id 字段 -->
                        <el-form-item label="群组" prop="chat_id">
                            <el-select v-model="form.chat_id" placeholder="请选择通知群组">
                                <el-option v-for="item in chatList" :key="item.id" :label="item.name"
                                    :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <!-- app_name 字段 -->
                        <el-form-item label="app名称" prop="app_name">
                            <el-input v-model="form.app_name" placeholder="请输入应用名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <!-- region 字段 -->
                        <el-form-item label="监控国家" prop="region">
                            <el-input v-model="form.region" placeholder="请输入国家/地区"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <!-- link 字段 -->
                    <el-form-item label="竞品链接" prop="link">
                        <el-input v-model="form.link" placeholder="请输入监控链接"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">{{ $t('tagsView.close') }}</el-button>
                <el-button type="warning" @click="affirm">{{ $t('table.confirm') }}</el-button>
            </div>
        </el-dialog>
        <el-dialog :visible.sync="dialogVisible1" @close="dialogVisible1 = false">
            <img :src="currentImageUrl" alt="Large Image" style="width: 100%;" />
        </el-dialog>
    </div>
</template>
<script>
import { getFeedback, getPkList, editFeedbackState } from "@/api/feedback";
import { getAppMonitorList, save, getHistoryList } from "@/api/appMonitor";
import pk from "@/utils/pk";
export default {
    name: "feedback",
    data() {
        return {
            languageContent: '',
            goalLanguage: "zh_CN",
            languageList: [],
            translateLoading: false,
            dialogVisible1: false,
            pkList: [],
            chatList: [
                {
                    id: 'oc_bcc0bf09efae4391e59fb1223c2523b6',
                    name: 'android-roku2'
                },
                {
                    id: 'oc_be46490aab46c35d7b001b85d172cdd6',
                    name: 'android-ins2'
                },
                {
                    id: 'oc_9c861e8af573f2192d243a23cb67e412',
                    name: 'ios-ruku'
                },
                {
                    id: 'oc_9c861e8af573f2192d243a23cb67e412',
                    name: 'ios-全能遥控'
                },
                {
                    id: 'oc_43a0f6d39cffe73d48ac5dd67ac0fba6',
                    name: 'ios-Wa'
                },
                {
                    id: 'oc_745941b33216de0b0ec0f778fc861e17',
                    name: 'ios-beauty'
                },
                {
                    id: 'oc_0f7e0a424d63200227dc2b032553d64a',
                    name: 'ios-心率'
                },
            ],
            stateList: [
                {
                    id: '0',
                    name: "已停用",
                },
                {
                    id: '1',
                    name: "已启用",
                },
            ],
            queryParams: {
                page: 1,
                size: 10,
                dateRange: {
                    start: "",
                    end: "",
                },
                status: "",
                package_name: "",
                link_id: ''
            },
            total: 0,
            loading: false,
            addLoading: false,
            moreArr: [],
            dialogVisible: false,
            form: {
                type: '',
                chat_id: '',
                app_name: '',
                link: '',
                region: '',
                id: ''
            },
            rules: {
                type: [
                    { required: true, message: '请选择类型', trigger: 'change' }
                ],
                chat_id: [
                    { required: true, message: '请选择通知群组', trigger: 'change' }
                ],
                app_name: [
                    { required: true, message: '请输入app标识', trigger: 'blur' }
                ],
                link: [
                    { required: true, message: '请输入竞品链接', trigger: 'blur' }
                ],
                region: [
                    { required: true, message: '请输入国家/地区', trigger: 'blur' }
                ]
            },
            search: "",
            value1: "",
            currentImageUrl:''
        };
    },
    mounted() {

    },

    created() {
        const { link_id } = this.$route.query;
        console.log(this.$route.query, 'this.$route.query')
        this.queryParams.id = link_id
        this.getMore();

        // this.getLanguageList();
    },
    methods: {
        goForm() {
            this.form = {}
            this.dialogVisible = true
        },
        copyEmail1(data) {
            const input = document.createElement('textarea');
            input.value = data;
            document.body.appendChild(input);
            input.select();
            try {
                document.execCommand('copy');
                this.$message({
                    message: 'Text copied to clipboard!',
                    type: 'success'
                });
            } catch (err) {
                this.$message({
                    message: 'Failed to copy text.',
                    type: 'error'
                });
            }
            document.body.removeChild(input);
        },
        timeChange() {
            this.queryParams.dateRange.start = this.value1[0];
            this.queryParams.dateRange.end = this.value1[1];
            this.handleQuery();
        },
        getKeyByValue(object, value) {
            return Object.keys(object).find((key) => object[key] === value);
        },
        async getMore() {
            this.loading = true;
            let { data, total } = await getHistoryList(this.queryParams);
            // if (data.length != 0) {
            //     // Create a map for quick lookup
            //     const chatMap = {};
            //     this.chatList.forEach(chat => {
            //         chatMap[chat.id] = chat.name;
            //     });

            //     // Assuming `data` is the array you want to process
            //     data.forEach(item => {
            //         item.chat_name = chatMap[item.chat_id] || item.chat_id; // Assign name or fallback to chat_id if not found
            //     });
            // }
            this.loading = false;
            this.moreArr = data;
            this.total = total;
        },
        async getLanguageList() {
            let { data } = await getLanguage({
                package_name: this.queryParams.package_name,
            });
            this.languageList = data.map((item) => ({
                id: item.reviewer_language,
                name: item.reviewer_language,
            }));
        },
        async translateHandel() {
            this.languageContent = "";
            this.translateLoading = true;
            let { data } = await gptTranslate({
                language: this.goalLanguage,
                translateText: this.form.description,
            });
            this.translateLoading = false;
            if (data.code == 0) {
                this.languageContent = data.data.answer;
            }
        },
        async getPkListHandel() {
            let { data } = await getAppVersionPkList({ page: 1, size: 999 });
            this.pkList = data;
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getMore();
        },
        async affirm() {
            this.$refs["ruleForm"].validate(async (valid) => {
                if (!valid) return;
                let res = await save(this.form);
                console.log(res)
                if (res.code == '0') {
                    this.addLoading = true;
                    this.addLoading = false;
                    this.$message.success("success");
                    this.getMore();
                    this.dialogVisible = false;
                } else {
                    this.$message.error(res.message);
                    this.addLoading = false;
                }

            });
        },
        openDialog(url) {
            this.currentImageUrl = url;
            this.dialogVisible1 = true;
        },
        handleEdit(i, row) {
            this.form = { ...row };
            this.dialogVisible = true;
        },
        handleDelete(index, row) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "error",
            }).then(async () => {
                row.state = 1
                const query = {
                    ...row
                }
                await save(query);
                this.getMore();
                this.$message({
                    message: "success",
                    type: "success",
                });
            });
        },
        handleChange(index, row) {
            let obj = JSON.parse(JSON.stringify(row))
            let text = `此操作将停用该数据, 是否继续?`
            if (obj.status == 0) {
                obj.status = 1
                text = `此操作将启用该数据, 是否继续?`
            } else {
                obj.status = 0
            }
            this.$confirm(text, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "error",
            }).then(async () => {
                const query = {
                    ...obj
                }
                await save(query);
                this.getMore();
                this.$message({
                    message: "success",
                    type: "success",
                });
            });
        },
        resetQuery() {
            this.queryParams.status = null;
            this.queryParams.package_name = null;
            this.queryParams.dateRange.start = null;
            this.queryParams.dateRange.end = null;
            this.queryParams.page = 1;
            this.value1 = "";
            this.getMore();
        },
    },
};
</script>
<style lang="scss" scoped>
.boxs {
    padding: 20px;
}

::v-deep.el-input.is-disabled .el-input__inner {
    color: #333 !important;
}

::v-deep.el-textarea.is-disabled .el-textarea__inner {
    color: #333 !important;
}

::v-deep .el-image {
    margin-right: 8px;
    border-radius: 4px;
}
</style>